<?php

class Fly_seach_helper{
  
  public static  function  createFlightSummayTableData($results) {
    $airline_company_array = array();
    $no_stops_flight_array = array();
    $stop_flight_array = array();
    $moreone_stop_flight = array();
    $i = 0;
    foreach ($results->air_price_solutions as $air_price_solution_item) {
	$flight_type = self::get_flight_type($air_price_solution_item);
	$airline_company = self::get_air_line_company_of_air_solution($air_price_solution_item, $results);
	if ($flight_type == "D") {
	    for ($k = 0; $k < 10; $k++) {

		if (!isset($airline_company_array[$k]) || $airline_company_array[$k] === $airline_company) {
		    if (!isset($no_stops_flight_array[$k])) {
			$airline_company_array[$k] = $airline_company;
			$no_stops_flight_array[$k] = $air_price_solution_item->apprixomate_total_price;
			break;
		    } else {
			break;
		    }
		}
	    }
	} else if ($flight_type == "A") {
	    for ($k = 0; $k < 10; $k++) {

		if (!isset($airline_company_array[$k]) || $airline_company_array[$k] === $airline_company) {
		    if (!isset($stop_flight_array[$k])) {
			$airline_company_array[$k] = $airline_company;
			$stop_flight_array[$k] = $air_price_solution_item->apprixomate_total_price;
			break;
		    } else {
			break;
		    }
		}
	    }
	} else if ($flight_type == "AA") {
	    for ($k = 0; $k < 10; $k++) {
		if (!isset($airline_company_array[$k]) || $airline_company_array[$k] === $airline_company) {
		    if (!isset($moreone_stop_flight[$k])) {
			$airline_company_array[$k] = $airline_company;
			$moreone_stop_flight[$k] = $air_price_solution_item->apprixomate_total_price;
			break;
		    } else {
			break;
		    }
		}
	    }
	}
    }
    $data = array("airline_company_array" => $airline_company_array, "no_stop_flight_array" => $no_stops_flight_array, "stop_flight_array" => $stop_flight_array, "moreone_stop_flight" => $moreone_stop_flight);
    return $data;
}

/**
 *
 * @param type $air_price_solution_item
 * @return D non-stop flight | A one-stop flight | AA more-than-onestop flight
 */
public static function get_flight_type($air_price_solution_item) {

    $flight_type_level_count = 0;
    foreach ($air_price_solution_item->journeys as $journey) {

	if (count($journey->air_segment_keys) == 2) {
	    if ($flight_type_level_count < 1) {
		$flight_type_level_count++;
	    }
	} else if (count($journey->air_segment_keys) > 2) {
	    if ($flight_type_level_count < 2) {
		$flight_type_level_count++;
	    }
	}
    }
    if ($flight_type_level_count == 0) {
	return "D";
    } else if ($flight_type_level_count == 1) {
	return "A";
    } else if ($flight_type_level_count == 2) {
	return "AA";
    }
}

public static function get_air_line_company_of_air_solution($air_price_solution_item, $results) {
    $first_air_line_company = null;
    foreach ($air_price_solution_item->journeys as $journey) {
	foreach ($journey->air_segment_keys as $airsegment_key) {
	    $air_segment_object = self::get_airsegment_by_key($airsegment_key, $results);
	    if (!isset($first_air_line_company)) {
		$first_air_line_company = $air_segment_object->carrier;
	    } else if ($first_air_line_company != $air_segment_object->carrier) {
		return "XXXX";
	    }
	}
    }
    return $first_air_line_company;
}

public static function get_airsegment_by_key($airsegment_key, $results) {
    $airsegment_objects = $results->air_segments;
    foreach ($airsegment_objects as $airsegment_object) {
	if ($airsegment_key === $airsegment_object->key) {
	    return $airsegment_object;
	}
    }
    return null;
}

public static function gets_airsegment_by_keys($airsegment_keys, $results) {
    $airsegment_objects = $results->air_segments;
    $found_airsegment_objects = array();
    foreach ($airsegment_objects as $airsegment_object) {
	foreach ($airsegment_keys as $airsegment_key) {
	    if ($airsegment_key === $airsegment_object->key) {
		array_push($found_airsegment_objects, $airsegment_object);
	    }
	}
    }
    usort($found_airsegment_objects, 'self::air_segment_compare');
    return $found_airsegment_objects;
}

public static function air_segment_compare($air_segment1, $air_segemnt2) {
    $ad = new DateTime($air_segment1->departure_time);
    $bd = new DateTime($air_segemnt2->departure_time);

    if ($ad == $bd) {
	return 0;
    }
    return $ad < $bd ? -1 : 1;
}

public static function  journey_travel_time_compare($journey1, $journey2) {
if ($journey1["travel_time"] == $journey2["travel_time"]) {
	return 0;
    }
    return $journey1["travel_time"] < $journey2["travel_time"] ? -1 : 1;
}


public static function create_unique_solution_key (){
    return substr(md5(uniqid(rand(), TRUE)), 0,15);
    
}

public static function getTotalMinuteFromUnixTimeStamp($seconds){
      return $seconds/60 + $seconds%60;
}
 

public static function getDepartureTimeAsTotalMinute($departure_time){
     $time = new DateTime($departure_time);
     $hours_string = $time->format("H:i");
     $hours_string_array = explode(":", $hours_string);
     return intval($hours_string_array[0])*60 + intval($hours_string_array[1]);
}





}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
